package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.AfterSaleOutHouseView;
import com.sl.au.entity.AfterSaleTicketTrance;
import com.sl.au.service.AfterSaleOutHouseService;
import com.sl.au.service.AfterSaleOutHouseViewService;

@RestController
@RequestMapping(value = "/ui/afterSaleOutHouseQuery")
public class AfterSaleOutHouseQueryController extends BaseController {

	@Autowired
	AfterSaleOutHouseService afterSaleOutHouseService;
	
	@Autowired
	AfterSaleOutHouseViewService afterSaleOutHouseViewService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("afterSaleOutHouse/outHousequeryindex");
		return modelAndView;
	}
	
	@RequestMapping(value = "/getpage", method = RequestMethod.GET)
	public Map<String, Object> getOutHousePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		List<AfterSaleOutHouseView> afterSaleOutHouses = null;
		long count = 0;
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String type = request.getParameter("type");
		if (key == null) {
			key = "";
		}
		condition.put("key", key);

		//condition.put("userId", userId);
		if(type.equals("1")){
			afterSaleOutHouses = afterSaleOutHouseViewService.getAllOutHouseTicket(start, length, condition, null, null);
			count = afterSaleOutHouseViewService.getRepository().getAllCountBykey(condition.get("key"));
		}
		if(type.equals("2")){
			afterSaleOutHouses = afterSaleOutHouseViewService.getYesOutHouseTicket(start, length, condition, null, null);
			count = afterSaleOutHouseViewService.getRepository().getYesCountBykey(condition.get("key"));
		}
		if(type.equals("3")){
			afterSaleOutHouses = afterSaleOutHouseViewService.getNoOutHouseTicket(start, length, condition, null, null);
			count = afterSaleOutHouseViewService.getRepository().getNoCountBykey(condition.get("key"));
		}
		map.put("data", afterSaleOutHouses);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "/getTrancePage", method = RequestMethod.GET)
	public Map<String, Object> getTrancePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String outId=request.getParameter("outId");
		String key=request.getParameter("key");
		if(key == null){
			key = "";
		}
		condition.put("outId", outId);
		condition.put("key", key);
		List<AfterSaleTicketTrance> afterSaleTicketTrances = afterSaleOutHouseService.findAllTranceBykey(start, length,
				condition, null, null);
		long count=afterSaleOutHouseService.getRepository().getTranceCountBykey(outId, key);
		map.put("data", afterSaleTicketTrances);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
}
